package com.dein.broadcast.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.dein.broadcast.model.Broadcast;
import com.dein.broadcast.service.BroadcastFavoriteService;

@Controller
@RequestMapping(value = "/broadcastFavorite")
public class BroadcastFavoriteController {

	@Autowired
	private BroadcastFavoriteService broadcastFavoriteService;

	private final static String TRUE = "{result:'true'}";
	private final static String FALSE = "{result:'false'}";

	@RequestMapping(value = "/addFavoriteBroadcast")
	public void addFavoriteBroadcast(@RequestParam int broadcastId,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		long userId = (Long) request.getSession().getAttribute("userId");
		PrintWriter out = response.getWriter();
		if (broadcastFavoriteService.addFavoriteBroadcast(userId, broadcastId)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/deleteFavoriteBroadcast")
	public void deleteFavoriteBroadcast(@RequestParam int broadcastId,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		long userId = (Long) request.getSession().getAttribute("userId");
		PrintWriter out = response.getWriter();
		if (broadcastFavoriteService.deleteFavoriteBroadcast(userId,
				broadcastId)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/getFavoriteBroadcastList")
	public List<Broadcast> getFavoriteBroadcastList(HttpServletRequest request) {
		long userId = (Long) request.getSession().getAttribute("userId");
		return broadcastFavoriteService.getFavoriteBroadcastList(userId);
	}

}
